package com.wx.controller.api;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import com.wx.client.SysFileClient;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.vo.IdCardVo;
import com.wx.exception.BusinessException;
import com.wx.properties.OcrProperties;
import com.wx.properties.OssProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>Title:      SysUploadFileController. </p>
 * <p>Description 文件管理 </p>
 * <p>Company:    北京医百科技有限公司 </p>
 *
 * @author         <a href="xinglinglove1029@163.com"/>杨文生</a>
 * @since      2020/9/10 17:51
 */
@RestController
@RequestMapping(value = "/api/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "文件管理(API)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
public class ApiUploadFileController {

    @Resource
    private OcrProperties ocrProperties;

    @Resource
    private SysFileClient sysFileClient;

    @Resource
    private OssProperties ossProperties;


    @PostMapping(value = "/tencentUpload")
    @ApiOperation(httpMethod = "POST", value = "上传文件(腾讯)")
    public Wrapper<String> txUpload(@RequestParam("file") MultipartFile multipartFile){
        String url = sysFileClient.uploadToTencent(multipartFile);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,url);
    }


//
//    @PostMapping(value = "/ocrAnalysis")
//    @ApiOperation(httpMethod = "POST", value = "证件照解析")
//    public Wrapper<IdCardVo> ocrAnalysis(@RequestParam("file") MultipartFile multipartFile){
//        try {
//            BASE64Encoder base64Encoder =new BASE64Encoder();
//            String base64EncoderImg = base64Encoder.encode(multipartFile.getBytes());
//            Credential cred = new Credential(ocrProperties.getSecretId(), ocrProperties.getSecretKey());
//
//            HttpProfile httpProfile = new HttpProfile();
//            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
//
//            ClientProfile clientProfile = new ClientProfile();
//            clientProfile.setHttpProfile(httpProfile);
//
//            OcrClient client = new OcrClient(cred, ocrProperties.getRegion(), clientProfile);
//
//            IDCardOCRRequest req = new IDCardOCRRequest();
//            req.setImageBase64(base64EncoderImg);
//
//            IDCardOCRResponse resp = client.IDCardOCR(req);
//            log.info("===> 证件照解析返回结果为result={}", IDCardOCRResponse.toJsonString(resp));
//
//            IdCardVo idCardVo = new IdCardVo();
//            BeanUtils.copyProperties(resp,idCardVo);
//            // 存储到对象存储中
//            String url = sysFileClient.uploadToTencent(multipartFile);
//            idCardVo.setIdCardUrl(ossProperties.getUrl() + url);
//            return WrapMapper.ok(idCardVo);
//        } catch (Exception e) {
//            throw new BusinessException(e.getMessage());
//        }
//    }

}
